home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Quaternary Source / HandleAppleEvents.cpp < prev    next >
Text File  |  1995-11-01  |  8KB  |  245 lines

  1. /* CODE EXAMPLE #1 */
  2. // Getting Files Selected from the Finder
  3. // A code example demonstrating how to get the files
  4. // that the user selected in the Finder before opening
  5. // your application.  You must have the High-Level
  6. // Event Aware flag in the application's 'SIZE' resource
  7. // set in order for the Apple Event code to work.
  8.  
  9. // NOTE: This code is taken (pretty much verbatim)
  10. // from Think Reference.
  11.  
  12. /*
  13.     8/15/94: Added handler for the quit event...
  14.     10/31/95: Made file independent and added some static globals, and
  15.               updated for compatibility with PowerPC compilation.
  16.     11/1/95: Added MultipleFileHandler capability.
  17. */
  18.  
  19. #include <GestaltEqu.h>
  20. #include <AppleEvents.h>
  21. #include "HandleAppleEvents.h"
  22.  
  23. typedef struct {
  24.     OpenFileHandler fileHandler;
  25.     PrintFileHandler printHandler;
  26.     QuitAppHandler quitHandler;
  27. } AppleEventStuff;
  28.  
  29. static AppleEventStuff sPrivAppleEvents;
  30.  
  31. // ---------------------------------------------------------------------------
  32.  
  33. OSErr MyGotRequiredParams (const AppleEvent *theAppleEvent);
  34.  
  35. pascal OSErr  AppHandleODoc (const AppleEvent *theAppleEvent, AppleEvent* reply, long
  36.                                                         handlerRefCon);
  37. pascal OSErr  AppHandlePDoc (const AppleEvent *theAppleEvent, AppleEvent *reply, long
  38.                                                         handlerRefCon);
  39. pascal OSErr  AppHandleOApp (const AppleEvent *theAppleEvent, AppleEvent *reply, long
  40.                                                         handlerRefCon);
  41. pascal OSErr AppHandleQApp  (const AppleEvent *theAppleEvent, AppleEvent *reply, long
  42.                                                         handlerRefCon);
  43.  
  44. // ---------------------------------------------------------------------------
  45.  
  46. Boolean AppleEventsInstalled () {
  47.     OSErr err;
  48.     long  result;
  49.  
  50.     // THINK C's MacTraps library provides glue for Gestalt, so
  51.     // it can be called safely under System 6. If an error is
  52.     // returned, then Gestalt for the AppleEvents Selector is
  53.     // not available (this also means that Apple Events are
  54.     // not available)
  55.     err = Gestalt (gestaltAppleEventsAttr, &result);
  56.     return (!err && ((result >> gestaltAppleEventsPresent) & 0x0001));
  57.                                             // return TRUE if there is no
  58.                                             // error and the proper bit of
  59.                                             // result is set
  60. } // END AppEventsInstalled
  61.  
  62. // ---------------------------------------------------------------------------
  63.  
  64. pascal OSErr  AppHandleODoc (const AppleEvent *theAppleEvent, AppleEvent* reply, long
  65.                                                         handlerRefCon) {
  66.     FSSpec myFSS;
  67.     AEDescList docList;
  68.     OSErr err;
  69.     long index, itemsInList;
  70.     Size actualSize;
  71.     AEKeyword keywd;
  72.     DescType returnedType;
  73.  
  74.     // get the direct parameter--a descriptor list--and put it into a docList
  75.     err = AEGetParamDesc (theAppleEvent, keyDirectObject,
  76.             typeAEList, &docList);
  77.     if (err)
  78.         return err;
  79.  
  80.     // check for missing parameters
  81.     err = MyGotRequiredParams (theAppleEvent);
  82.     if (err)
  83.         return err;
  84.  
  85.     // count the number of descriptor records in the list
  86.     err = AECountItems (&docList, &itemsInList);
  87.  
  88.     // now get each descriptor record from the list, coerce the returned
  89.     // data to an FSSpec record, and open the associated file
  90.     
  91.     // In our app, we can open multiple files.
  92.     // Comment out below to handle only the first file...
  93.     //if (itemsInList > 0)
  94.     //    itemsInList = 1;    // Handle only 1
  95.  
  96.     if (sPrivAppleEvents.fileHandler != NULL && itemsInList > 0) {
  97.         FSSpec *fileList;
  98.         fileList = (FSSpec*)NewPtr(sizeof(FSSpec) * itemsInList);
  99.  
  100.         if (fileList != NULL) {
  101.             for (index = 1; index <= itemsInList; index++) {
  102.                 err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  103.                     &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  104.                 if (err)
  105.                     return err;
  106.                 BlockMove(&myFSS, &fileList[index-1], sizeof(FSSpec));
  107.             }
  108.             (sPrivAppleEvents.fileHandler)(itemsInList, fileList);
  109.             DisposePtr((Ptr)fileList);
  110.         }
  111.     }
  112.     /*
  113.     else
  114.         for (index = 1; index <= itemsInList; index++) {
  115.             err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  116.                 &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  117.             if (err)
  118.                 return err;
  119.             if (sPrivAppleEvents.fileHandler != NULL)
  120.                 (*sPrivAppleEvents.fileHandler)(&myFSS);
  121.         }
  122.     */
  123.  
  124.     err = AEDisposeDesc (&docList);
  125.     return noErr;
  126. } // END AppHandleODoc
  127.  
  128. // ---------------------------------------------------------------------------
  129.  
  130. pascal OSErr  AppHandlePDoc (const AppleEvent *theAppleEvent, AppleEvent *reply, long
  131.                                                         handlerRefCon) {
  132.     FSSpec myFSS;
  133.     AEDescList docList;
  134.     OSErr err;
  135.     long index, itemsInList;
  136.     Size actualSize;
  137.     AEKeyword keywd;
  138.     DescType returnedType;
  139.  
  140.     // get the direct parameter--a descriptor list--and put it into a docList
  141.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
  142.                 &docList);
  143.     if (err)
  144.         return err;
  145.  
  146.     // check for missing parameters
  147.     err = MyGotRequiredParams (theAppleEvent);
  148.     if (err)
  149.         return err;
  150.  
  151.     // count the number of descriptor records in the list
  152.     err = AECountItems (&docList, &itemsInList);
  153.  
  154.     // now get each descriptor record from the list, coerce the returned
  155.     // data to an FSSpec record, and open the associated file
  156.     for (index = 1; index <= itemsInList; index++) {
  157.         err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  158.             &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  159.             if (err)
  160.                 return err;
  161.             if (sPrivAppleEvents.printHandler != NULL)
  162.                 (*sPrivAppleEvents.printHandler)(&myFSS);
  163.     }
  164.  
  165.     err = AEDisposeDesc (&docList);
  166.     return noErr;
  167. } // AppHandlePDoc
  168.  
  169. // ---------------------------------------------------------------------------
  170.  
  171.  
  172. pascal OSErr  AppHandleOApp (const AppleEvent *theAppleEvent, AppleEvent *reply, long
  173.                                                         handlerRefCon) {
  174.     // Do nothing in this case
  175.     return(noErr);
  176. } // END AppHandleOApp
  177.  
  178. // ---------------------------------------------------------------------------
  179.  
  180. // Quit event. DO NOT CALL EXITTOSHELL. The Finder will die an unmerciful death!
  181.  
  182. pascal OSErr AppHandleQApp  (const AppleEvent *theAppleEvent, AppleEvent *reply, long
  183.                                                         handlerRefCon) {
  184.     if (sPrivAppleEvents.quitHandler != NULL)
  185.         (*sPrivAppleEvents.quitHandler)();
  186.  
  187.     return(noErr);
  188. } // END AppHandleQApp
  189.  
  190. // ---------------------------------------------------------------------------
  191.  
  192. OSErr MyGotRequiredParams (const AppleEvent *theAppleEvent) {
  193.     DescType returnedType;
  194.     Size actualSize;
  195.     OSErr err;
  196.  
  197.     err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
  198.                 typeWildCard, &returnedType, nil, 0, &actualSize);
  199.     if (err == errAEDescNotFound)    // you got all the required parameters
  200.         return noErr;
  201.     else if (!err)                    // you missed a required parameter
  202.         return errAEEventNotHandled;
  203.     else                            // the call to AEGetAttributePtr failed
  204.         return err;
  205. } // END MyGotRequiredParams
  206.  
  207. // ---------------------------------------------------------------------------
  208.  
  209. void InitAppleEvents(
  210.     OpenFileHandler        fileHandler,
  211.     PrintFileHandler    printHandler,
  212.     QuitAppHandler        quitHandler) {
  213.  
  214.     if (AppleEventsInstalled()) {
  215.         OSErr err;
  216.         AEEventHandlerUPP aeFileHandler;
  217.         AEEventHandlerUPP aePrintHandler;
  218.         AEEventHandlerUPP aeOpenHandler;
  219.         AEEventHandlerUPP aeQuitHandler;
  220.  
  221.         aeFileHandler  = NewAEEventHandlerProc(AppHandleODoc);
  222.         aePrintHandler = NewAEEventHandlerProc(AppHandlePDoc);
  223.         aeOpenHandler  = NewAEEventHandlerProc(AppHandleOApp);
  224.         aeQuitHandler  = NewAEEventHandlerProc(AppHandleQApp);
  225.     
  226.         sPrivAppleEvents.fileHandler = fileHandler;
  227.         sPrivAppleEvents.printHandler = printHandler;
  228.         sPrivAppleEvents.quitHandler = quitHandler;
  229.  
  230.         err = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
  231.                                     aeFileHandler, 0, false);
  232.         err = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
  233.                                     aeOpenHandler, 0, false);
  234.         err = AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments,
  235.                                     aePrintHandler, 0, false);
  236.         err = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
  237.                                     aeQuitHandler, 0, false);
  238.     }
  239. } // END InitAppleEvents
  240.  
  241.  
  242. // ==========================================================================
  243. // ==========================================================================
  244. // END HandleAppleEvents.c++
  245.